package com.tma.basicfragment.music;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import a.thread.AsyncTaskWrapper;
import a.thread.ThreadUtil;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.tma.basicfragment.R;

public class LoadSongTask extends AsyncTaskWrapper<Void, Void, LoadSongResult> {

    private static final String TAG = "LoadSongTask";

    public LoadSongTask(Activity activity) {
        super(LoadSongTask.class.getSimpleName(), activity);
    }

    @Override
    protected LoadSongResult doInBackground(Void... params) {
        SongParser parser = new SongParser();
        List<Song> songs = null;
        Activity activity = (Activity) mActivity.get();
        try {
            // TODO REMOVE
            ThreadUtil.sleepForInSecs(5);

            InputStream stream = activity.getResources().openRawResource(R.raw.music);
            songs = parser.parse(stream);
        } catch (XmlPullParserException e) {
            Log.e(TAG, e.getMessage(), e);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return new LoadSongResult(songs);
    }

    @Override
    protected ProgressDialog createProgressDialog() {
        if (mActivity.get() == null) {
            Log.e(TAG, "Activity is null. Can not create ProgressDialog");
            return null;
        }
        ProgressDialog pd = new ProgressDialog((Context) mActivity.get());
        pd.setTitle("Please wait");
        pd.setMessage("Processing songs...");
        pd.setCancelable(true);
        return pd;
    }
}
